package com.yrcm.eduapp.util;

import sanyuan.any123.api.identity.User;
import android.content.Context;
import android.content.SharedPreferences;

public class PreferencesUtil {
	public static final String PREFERENCE_LOGIN_TOKEN = "token";
	public static final String PREFERENCE_LOGIN_USERNAME = "user_name";
	public static final String PREFERENCE_LOGIN_PASSWORD = "password";
	public static final String PREFERENCE_USER_ID = "user";

	public static final String PREFERENCE_USER_NAME = "userrealname";
	public static final String PREFERENCE_USER_GRADE = "usergrade";
	public static final String PREFERENCE_USER_GENDER = "usergender";
	public static final String PREFERENCE_USER_SCHOOL = "userSchool";
	public static final String PREFERENCE_USER_BD_NUM = "usernum";

	public PreferencesUtil(Context context) {
		// SharedPreferences share = context.getSharedPreferences(arg0, arg1)

	}

	/**
	 * save token
	 * 
	 * @param token
	 */
	public static void saveToken(final SharedPreferences.Editor editor,
			String token) {
		editor.putString(PREFERENCE_LOGIN_TOKEN, token);
		editor.commit();
	}

	public static String getToken(SharedPreferences prefs) {
		return prefs.getString(PREFERENCE_LOGIN_TOKEN, "");

	}

	public static void saveUser(final SharedPreferences.Editor editor,
			String userName, String pwd) {
		editor.putString(PREFERENCE_LOGIN_USERNAME, userName);
		editor.putString(PREFERENCE_LOGIN_PASSWORD, pwd);
		editor.commit();
	}

	public static String getUserName(SharedPreferences prefs) {
		return prefs.getString(PREFERENCE_LOGIN_USERNAME, "");
	}

	public static String getPassword(SharedPreferences prefs) {
		return prefs.getString(PREFERENCE_LOGIN_PASSWORD, "");
	}

	public static void savePassword(SharedPreferences.Editor editor, String pwd) {
		editor.putString(PREFERENCE_LOGIN_PASSWORD, pwd);
		editor.commit();
	}

	public static void saveUserId(SharedPreferences.Editor editor, long uID) {
		editor.putLong(PREFERENCE_USER_ID, uID);
		editor.commit();
	}

	public static long getUserId(SharedPreferences prefs) {
		return prefs.getLong(PREFERENCE_USER_ID, 0L);
	}

	public static void loginOut(SharedPreferences.Editor editor) {
		editor.remove(PREFERENCE_LOGIN_TOKEN);
		// editor.remove(PREFERENCE_LOGIN_PASSWORD);
		// editor.remove(PREFERENCE_LOGIN_USERNAME);
		// editor.remove(PREFERENCE_USER_ID);
		editor.commit();
	}

	public static void saveUserInfo(SharedPreferences.Editor editor, User user) {
		editor.putString(PREFERENCE_USER_NAME, user.getRealName());
		editor.putInt(PREFERENCE_USER_GRADE, user.getGrade().getValue());
		editor.putString(PREFERENCE_USER_SCHOOL, user.getSchool());
		editor.putInt(PREFERENCE_USER_GENDER, user.getGender().getValue());
		editor.commit();
	}

	public static int getUserGender(SharedPreferences prefs) {
		return prefs.getInt(PREFERENCE_USER_GENDER, 0);
	}

	public static int getUserGrade(SharedPreferences prefs) {
		return prefs.getInt(PREFERENCE_USER_GRADE, 0);
	}

	public static String getUserSchool(SharedPreferences prefs) {
		return prefs.getString(PREFERENCE_USER_SCHOOL, "");
	}

	public static String getUserRealName(SharedPreferences prefs) {
		return prefs.getString(PREFERENCE_USER_NAME,"");
	}
	
	public static void saveUserBDNum(SharedPreferences.Editor editor, String num) {
		editor.putString(PREFERENCE_USER_BD_NUM, num);
		editor.commit();
	}
	public static String getUserBDNum(SharedPreferences prefs) {
		return prefs.getString(PREFERENCE_USER_BD_NUM,"");
	}
}
